<?php
/**
 * Created by PhpStorm.
 * User: Progress
 * Date: 2019/10/28
 * Time: 17:19
 */
namespace app\api\controller;

use app\admin\model\ArticleAnnouncement;
use app\admin\model\ArticleNews;
use app\admin\model\ArticlePlatform;
use app\admin\model\ArticlePop;
use app\admin\model\ArticlePopUser;
use app\admin\model\Banner;
use app\common\controller\Api;
use app\common\model\Config;

/**
 * 系统文章
 */
class Article extends Api
{
    protected $noNeedLogin = ['getBanner','getRollMessage','getAnnouncementList','getAnnouncementDetail'];
    protected $noNeedRight = ['*'];


    /**
     * 得到banner
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBanner()
    {
        $banner = Banner::field('id,image')->order('sort','desc')->select();

        $this->success('ok',$banner);
    }

    /**
     * banner 详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBannerDetail()
    {
        $id = $this->request->get('id');
        $data = Banner::where('id',$id)->find();
        $this->success('ok',$data);
    }

    /**
     * 滚动消息
     */
    public function getRollMessage()
    {
        $data = ArticleAnnouncement::field('id,title')->where('status',1)->order('id','desc')->select();
        $this->success('ok',$data);
    }

    /**
     * 公告列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAnnouncementList()
    {
        $data = ArticleAnnouncement::field('id,title,introduce,update_time')
            ->where('status',1)->order('update_time','desc')->select();
//        $tmp = [];
        foreach ($data as $value) {
            $value->update_time = date('Y年m月d日',strtotime($value->update_time));
//            $tmp[date('Ym',strtotime($value->update_time))]['str'] =  date('Y年m月',strtotime($value->update_time));
//            $tmp[date('Ym',strtotime($value->update_time))]['data'][] = $value;
        }
        $this->success('ok',$data);
    }

    /**
     * 公告详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAnnouncementDetail()
    {
        $id = $this->request->param('id');

        $data = ArticleAnnouncement::where('id',$id)->where('status',1)->find();

        $data->update_time = date('Y年m月d日',strtotime($data->update_time));
        $this->success('ok',$data);
    }

    /**
     * 首页资讯列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsList()
    {
        $page = $this->request->get('page') ?? 1;

        $data = ArticleNews::field('id,image,title,introduce,update_time')
            ->where('status',1)
            ->page($page,20)
            ->order('update_time','desc')->select();

        $this->success('ok',$data);
    }

    /**
     * 咨询详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsDetail()
    {
        $id = $this->request->get('id');

        $data = ArticleNews::where('id',$id)->where('status',1)->find();

        $this->success('ok',$data);
    }

    /**
     * 获取系统文章
     */
    public function getPlatformArticle()
    {
        $type = $this->request->get('type');
        $data = ArticlePlatform::where('key',$type)->find();

        $this->success('data',$data);
    }

    /**
     * 得到系统公告
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPop()
    {
        $data = ArticlePop::field('id,title,,introduce,content,update_time')
            ->where('status',1)->order('update_time','desc')->find();
        if (!empty($data)) {
            //如果文章没有更新过则不弹；
            $pop_user = ArticlePopUser::where('article_pop_id',$data->id)
                ->where('user_id',$this->auth->getUser()->id)
                ->where('update_time',$data->update_time)->find();
            if (empty($pop_user)) {
                ArticlePopUser::create([
                    'user_id' => $this->auth->getUser()->id,
                    'article_pop_id' => $data->id,
                    'update_time' => $data->update_time
                ]);
            } else {            //如果弹过则不弹
                $data = [];
            }
        }
        $this->success('ok',$data);
    }
}